using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;

public class PlayerConnector : NetworkBehaviour
{
	[SyncVar]
	public GameObject PlayingCharacter;

	public NetworkInstanceId NetID;

	public float SpawnwDelay = 1f;

	public bool AutoSpawn = true;

	public bool FindPlayerObject;

	[SyncVar]
	public int ConnectID = -1;

	private float timetmp;

	private NetworkInstanceId ___PlayingCharacterNetId;

	private static int kCmdCmdRequestAuthorizedCharacter;

	private static int kCmdCmdRequestSpawnPlayer;

	private static int kCmdCmdRequestSpawnWithTeam;

	private static int kCmdCmdTellServerMyInfo;

	private static int kCmdCmdTellServerMyCharacter;

	public GameObject NetworkPlayingCharacter
	{
		get
		{
			return PlayingCharacter;
		}
		[param: In]
		set
		{
			SetSyncVarGameObject(value, ref PlayingCharacter, 1u, ref ___PlayingCharacterNetId);
		}
	}

	public int NetworkConnectID
	{
		get
		{
			return ConnectID;
		}
		[param: In]
		set
		{
			SetSyncVar(value, ref ConnectID, 2u);
		}
	}

	private void Start()
	{
		timetmp = Time.time;
		UnityEngine.Debug.Log("Spawn Player connecter server " + base.isServer);
	}

	public override void OnStartLocalPlayer()
	{
		GetNetID();
		base.OnStartLocalPlayer();
	}

	private void Update()
	{
		if (!base.isLocalPlayer || ConnectID == -1)
		{
			return;
		}
		UnitZ.gameManager.PlayerNetID = ConnectID;
		if (base.isLocalPlayer && base.isServer && UnitZ.gameNetwork.IsDedicatedServer)
		{
			if (UnitZ.dedicatedManager != null)
			{
				UnitZ.dedicatedManager.CurrentPlayer = this;
			}
			return;
		}
		if (PlayingCharacter == null && UnitZ.playerManager.PlayingCharacter != null)
		{
			NetworkPlayingCharacter = UnitZ.playerManager.PlayingCharacter.gameObject;
			if (PlayingCharacter != null)
			{
				CallCmdTellServerMyCharacter(PlayingCharacter.gameObject);
			}
		}
		if (!base.isLocalPlayer)
		{
			return;
		}
		if (FindPlayerObject)
		{
			if (UnitZ.playerManager.PlayingCharacter != null)
			{
				CallCmdRequestAuthorizedCharacter(UnitZ.gameManager.UserID, UnitZ.gameManager.PlayerNetID);
				UnitZ.playerManager.PlayingCharacter.SendMessage("OnPlayerAuthorized", SendMessageOptions.DontRequireReceiver);
				UnityEngine.Object.Destroy(base.gameObject);
			}
		}
		else if (PlayingCharacter == null && AutoSpawn && Time.time > timetmp + SpawnwDelay)
		{
			CallCmdRequestSpawnPlayer(Vector3.zero, ConnectID, UnitZ.gameManager.UserID, UnitZ.gameManager.UserName, UnitZ.characterManager.CharacterIndex, UnitZ.gameManager.CharacterKey, -1);
			UnityEngine.Object.Destroy(base.gameObject);
			timetmp = Time.time;
		}
	}

	public void RequestSpawnWithTeam(byte team, int spawnpoint)
	{
		CallCmdRequestSpawnWithTeam(Vector3.zero, ConnectID, UnitZ.gameManager.UserID, UnitZ.gameManager.UserName, UnitZ.characterManager.CharacterIndex, UnitZ.gameManager.CharacterKey, team, spawnpoint);
		UnityEngine.Object.Destroy(base.gameObject);
	}

	public void RequestSpawn(int spawnpoint)
	{
		CallCmdRequestSpawnPlayer(Vector3.zero, ConnectID, UnitZ.gameManager.UserID, UnitZ.gameManager.UserName, UnitZ.characterManager.CharacterIndex, UnitZ.gameManager.CharacterKey, spawnpoint);
		UnityEngine.Object.Destroy(base.gameObject);
	}

	[Command]
	public void CmdRequestAuthorizedCharacter(string userID, int connectID)
	{
		UnitZ.gameNetwork.RequestAuthorizedCharacter(userID, connectID, base.connectionToClient);
		NetworkServer.Destroy(base.gameObject);
	}

	[Command]
	public void CmdRequestSpawnPlayer(Vector3 position, int connectid, string userid, string usename, int characterindex, string characterkey, int spawn)
	{
		UnitZ.gameNetwork.RequestSpawnPlayer(position, connectid, userid, usename, characterindex, characterkey, 0, spawn, base.connectionToClient);
		NetworkServer.Destroy(base.gameObject);
	}

	[Command]
	public void CmdRequestSpawnWithTeam(Vector3 position, int connectid, string userid, string usename, int characterindex, string characterkey, byte team, int spawn)
	{
		UnitZ.gameNetwork.RequestSpawnPlayer(position, connectid, userid, usename, characterindex, characterkey, team, spawn, base.connectionToClient);
		NetworkServer.Destroy(base.gameObject);
	}

	[Client]
	private void GetNetID()
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogWarning("[Client] function 'System.Void PlayerConnector::GetNetID()' called on server");
			return;
		}
		NetID = GetComponent<NetworkIdentity>().netId;
		NetworkConnectID = (int)NetID.Value;
		CallCmdTellServerMyInfo((int)NetID.Value, UnitZ.gameManager.UserName, UnitZ.gameManager.Team, UnitZ.GameKeyVersion);
	}

	[Command]
	private void CmdTellServerMyInfo(int id, string username, string team, string gamekey)
	{
		NetworkConnectID = id;
		if ((bool)UnitZ.NetworkGameplay && (bool)UnitZ.NetworkGameplay.playersManager)
		{
			UnitZ.NetworkGameplay.playersManager.UpdatePlayerInfo(id, 0, 0, username, team, gamekey, isconnected: true);
		}
		UnityEngine.Debug.Log("NetID " + id + " has connect to the server");
	}

	[Command]
	private void CmdTellServerMyCharacter(GameObject player)
	{
		NetworkPlayingCharacter = player;
	}

	private void UNetVersion()
	{
	}

	protected static void InvokeCmdCmdRequestAuthorizedCharacter(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRequestAuthorizedCharacter called on client.");
		}
		else
		{
			((PlayerConnector)obj).CmdRequestAuthorizedCharacter(reader.ReadString(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdRequestSpawnPlayer(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRequestSpawnPlayer called on client.");
		}
		else
		{
			((PlayerConnector)obj).CmdRequestSpawnPlayer(reader.ReadVector3(), (int)reader.ReadPackedUInt32(), reader.ReadString(), reader.ReadString(), (int)reader.ReadPackedUInt32(), reader.ReadString(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdRequestSpawnWithTeam(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdRequestSpawnWithTeam called on client.");
		}
		else
		{
			((PlayerConnector)obj).CmdRequestSpawnWithTeam(reader.ReadVector3(), (int)reader.ReadPackedUInt32(), reader.ReadString(), reader.ReadString(), (int)reader.ReadPackedUInt32(), reader.ReadString(), (byte)reader.ReadPackedUInt32(), (int)reader.ReadPackedUInt32());
		}
	}

	protected static void InvokeCmdCmdTellServerMyInfo(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdTellServerMyInfo called on client.");
		}
		else
		{
			((PlayerConnector)obj).CmdTellServerMyInfo((int)reader.ReadPackedUInt32(), reader.ReadString(), reader.ReadString(), reader.ReadString());
		}
	}

	protected static void InvokeCmdCmdTellServerMyCharacter(NetworkBehaviour obj, NetworkReader reader)
	{
		if (!NetworkServer.active)
		{
			UnityEngine.Debug.LogError("Command CmdTellServerMyCharacter called on client.");
		}
		else
		{
			((PlayerConnector)obj).CmdTellServerMyCharacter(reader.ReadGameObject());
		}
	}

	public void CallCmdRequestAuthorizedCharacter(string userID, int connectID)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRequestAuthorizedCharacter called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRequestAuthorizedCharacter(userID, connectID);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRequestAuthorizedCharacter);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(userID);
		networkWriter.WritePackedUInt32((uint)connectID);
		SendCommandInternal(networkWriter, 0, "CmdRequestAuthorizedCharacter");
	}

	public void CallCmdRequestSpawnPlayer(Vector3 position, int connectid, string userid, string usename, int characterindex, string characterkey, int spawn)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRequestSpawnPlayer called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRequestSpawnPlayer(position, connectid, userid, usename, characterindex, characterkey, spawn);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRequestSpawnPlayer);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(position);
		networkWriter.WritePackedUInt32((uint)connectid);
		networkWriter.Write(userid);
		networkWriter.Write(usename);
		networkWriter.WritePackedUInt32((uint)characterindex);
		networkWriter.Write(characterkey);
		networkWriter.WritePackedUInt32((uint)spawn);
		SendCommandInternal(networkWriter, 0, "CmdRequestSpawnPlayer");
	}

	public void CallCmdRequestSpawnWithTeam(Vector3 position, int connectid, string userid, string usename, int characterindex, string characterkey, byte team, int spawn)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdRequestSpawnWithTeam called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdRequestSpawnWithTeam(position, connectid, userid, usename, characterindex, characterkey, team, spawn);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdRequestSpawnWithTeam);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(position);
		networkWriter.WritePackedUInt32((uint)connectid);
		networkWriter.Write(userid);
		networkWriter.Write(usename);
		networkWriter.WritePackedUInt32((uint)characterindex);
		networkWriter.Write(characterkey);
		networkWriter.WritePackedUInt32(team);
		networkWriter.WritePackedUInt32((uint)spawn);
		SendCommandInternal(networkWriter, 0, "CmdRequestSpawnWithTeam");
	}

	public void CallCmdTellServerMyInfo(int id, string username, string team, string gamekey)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdTellServerMyInfo called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdTellServerMyInfo(id, username, team, gamekey);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdTellServerMyInfo);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.WritePackedUInt32((uint)id);
		networkWriter.Write(username);
		networkWriter.Write(team);
		networkWriter.Write(gamekey);
		SendCommandInternal(networkWriter, 0, "CmdTellServerMyInfo");
	}

	public void CallCmdTellServerMyCharacter(GameObject player)
	{
		if (!NetworkClient.active)
		{
			UnityEngine.Debug.LogError("Command function CmdTellServerMyCharacter called on server.");
			return;
		}
		if (base.isServer)
		{
			CmdTellServerMyCharacter(player);
			return;
		}
		NetworkWriter networkWriter = new NetworkWriter();
		networkWriter.Write((short)0);
		networkWriter.Write((short)5);
		networkWriter.WritePackedUInt32((uint)kCmdCmdTellServerMyCharacter);
		networkWriter.Write(GetComponent<NetworkIdentity>().netId);
		networkWriter.Write(player);
		SendCommandInternal(networkWriter, 0, "CmdTellServerMyCharacter");
	}

	static PlayerConnector()
	{
		kCmdCmdRequestAuthorizedCharacter = 95661419;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerConnector), kCmdCmdRequestAuthorizedCharacter, InvokeCmdCmdRequestAuthorizedCharacter);
		kCmdCmdRequestSpawnPlayer = -1436661543;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerConnector), kCmdCmdRequestSpawnPlayer, InvokeCmdCmdRequestSpawnPlayer);
		kCmdCmdRequestSpawnWithTeam = -472275909;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerConnector), kCmdCmdRequestSpawnWithTeam, InvokeCmdCmdRequestSpawnWithTeam);
		kCmdCmdTellServerMyInfo = 1834819482;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerConnector), kCmdCmdTellServerMyInfo, InvokeCmdCmdTellServerMyInfo);
		kCmdCmdTellServerMyCharacter = 1795945405;
		NetworkBehaviour.RegisterCommandDelegate(typeof(PlayerConnector), kCmdCmdTellServerMyCharacter, InvokeCmdCmdTellServerMyCharacter);
		NetworkCRC.RegisterBehaviour("PlayerConnector", 0);
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		if (forceAll)
		{
			writer.Write(PlayingCharacter);
			writer.WritePackedUInt32((uint)ConnectID);
			return true;
		}
		bool flag = false;
		if ((base.syncVarDirtyBits & 1) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.Write(PlayingCharacter);
		}
		if ((base.syncVarDirtyBits & 2) != 0)
		{
			if (!flag)
			{
				writer.WritePackedUInt32(base.syncVarDirtyBits);
				flag = true;
			}
			writer.WritePackedUInt32((uint)ConnectID);
		}
		if (!flag)
		{
			writer.WritePackedUInt32(base.syncVarDirtyBits);
		}
		return flag;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
		if (initialState)
		{
			___PlayingCharacterNetId = reader.ReadNetworkId();
			ConnectID = (int)reader.ReadPackedUInt32();
			return;
		}
		int num = (int)reader.ReadPackedUInt32();
		if ((num & 1) != 0)
		{
			PlayingCharacter = reader.ReadGameObject();
		}
		if ((num & 2) != 0)
		{
			ConnectID = (int)reader.ReadPackedUInt32();
		}
	}

	public override void PreStartClient()
	{
		if (!___PlayingCharacterNetId.IsEmpty())
		{
			NetworkPlayingCharacter = ClientScene.FindLocalObject(___PlayingCharacterNetId);
		}
	}
}
